package com.sixth.service.impl;

import com.github.pagehelper.util.StringUtil;
import com.sixth.annotation.PageInfoAdvice;
import com.sixth.bean.MarketAdmin;
import com.sixth.bean.MarketAdminExample;
import com.sixth.bean.MarketNoticeAdmin;
import com.sixth.bean.MarketNoticeAdminExample;
import com.sixth.bean.bo.AdminProfilePasswordBo;
import com.sixth.bean.common.BasePageInfo;
import com.sixth.constant.Constant;
import com.sixth.mapper.MarketAdminMapper;
import com.sixth.mapper.MarketNoticeAdminMapper;
import com.sixth.service.AdminProfileService;
import com.sixth.util.EncryptionUtil;
import com.sixth.util.MarketInfoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @ClassName AdminProfileServiceImpl
 * @Description TODO
 * @Author XiaoWJ
 * @Date 2022/9/9 19:54
 **/
@Transactional
@Service
public class AdminProfileServiceImpl implements AdminProfileService {

    @Autowired
    MarketAdminMapper marketAdminMapper;

    @Autowired
    MarketNoticeAdminMapper marketNoticeAdminMapper;


    @Override
    public int password(AdminProfilePasswordBo adminProfilePasswordBo) {

        // 从subject中获取当前管理员对应的信息
        MarketAdmin marketAdmin = MarketInfoUtil.getMarketAdmin();


        MarketAdminExample example = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = example.createCriteria();

        criteria.andIdEqualTo(marketAdmin.getId());

        // 对旧密码进行加密后再比对
        String username = marketAdmin.getUsername();
        String password = EncryptionUtil.getMD5String(adminProfilePasswordBo.getOldPassword(), username);
        criteria.andPasswordEqualTo(password).andDeletedEqualTo(false);

        // 查找旧密码是否正确
        List<MarketAdmin> admins = marketAdminMapper.selectByExample(example);

        // 旧密码不正确，返回
        if (admins.size() == 0) {
            return Constant.NON;
        }

        // 旧密码正确，修改为新密码
        MarketAdmin marketAdmin1 = new MarketAdmin();
        marketAdmin1.setId(marketAdmin.getId());

        // 对新密码进行加密加盐
        String newPwd = EncryptionUtil.getMD5String(adminProfilePasswordBo.getNewPassword(), username);
        marketAdmin1.setPassword(newPwd);

        int affectRows = marketAdminMapper.updateByPrimaryKeySelective(marketAdmin1);

        return affectRows > 0 ? Constant.OK : Constant.NON;
    }

    @PageInfoAdvice
    @Override
    public Object lsnotice(BasePageInfo basePageInfo, String type, String title) {
        MarketNoticeAdminExample example = new MarketNoticeAdminExample();

        // 排序
        example.setOrderByClause(basePageInfo.getSort() + " " + basePageInfo.getOrder());

        MarketNoticeAdminExample.Criteria criteria = example.createCriteria();

        criteria.andDeletedEqualTo(false);

        // type为all,不增加该筛选条件
        if (!StringUtil.isEmpty(type)) {
            // 已读，readTime不为空
            if ("read".equals(type)) {
                criteria.andReadTimeIsNotNull();
            } else if ("unread".equals(type)) {
                criteria.andReadTimeIsNull();
            }
        }
        // title做模糊查询
        if (!StringUtil.isEmpty(title)) {
            criteria.andNoticeTitleLike("%" + title.trim() + "%");
        }

        List<MarketNoticeAdmin> marketNoticeAdmins = marketNoticeAdminMapper.selectByExample(example);

        return marketNoticeAdmins;
    }

    @Override
    public int catnotice(MarketNoticeAdmin marketNoticeAdmin) {

        // 修改为已读，并且更新阅读时间
        Date date = new Date();

        marketNoticeAdmin.setReadTime(date);
        //marketNoticeAdmin.setUpdateTime(date);

        int affectRows = marketNoticeAdminMapper.updateByPrimaryKeySelective(marketNoticeAdmin);

        return affectRows > 0 ? Constant.OK : Constant.NON;
    }

    @Override
    public int bcatnotice(List<Integer> ids) {
        Date readTime = new Date();
        int affectRows = marketNoticeAdminMapper.updateReadTimeByIds(ids, readTime);

        return affectRows == ids.size() ? Constant.OK : Constant.NON;
    }

    @Override
    public int brmnotice(List<Integer> ids) {

        int affectRows = marketNoticeAdminMapper.deleteAdminNotice(ids);

        return affectRows == ids.size() ? Constant.OK : Constant.NON;
    }
}
